import java.util.Scanner;

public class HomeWork0824 {
	public static void main(String[] args) {
		hw92();
	}

	/**
	 * 1
	 */
	public static void bookTest() {
		Book book1 = new Book();
		book1.id = 1001;
		book1.title = "唐诗三百首";
		book1.price = 15.8;
		book1.publisher = "晨光出版社";
		book1.showMe();
		Book book2 = new Book();
		book2.id = 1002;
		book2.title = "新365夜好故事";
		book2.price = 15.0;
		book2.publisher = "上海人民美术出版社";
		book2.showMe();
	}

	/**
	 * 2
	 */
	public static void circleTest() {
		Circle circle1 = new Circle();
		circle1.radius = 6;
		System.out.println("面积：" + circle1.getArea());
		System.out.println("周长：" + circle1.getPerimeter());
		Circle circle2 = new Circle();
		circle2.radius = 10;
		System.out.println("面积：" + circle2.getArea());
		System.out.println("周长：" + circle2.getPerimeter());
	}

	/**
	 * 3
	 */
	public static void rectangleTest() {
		Rectangle rectangle1 = new Rectangle();
		rectangle1.height = 5;
		rectangle1.width = 10;
		System.out.println("面积：" + rectangle1.getArea());
		System.out.println("周长：" + rectangle1.getPerimeter());

		Rectangle rectangle2 = new Rectangle();
		rectangle2.height = 15.8;
		rectangle2.width = 9.8;
		System.out.println("面积：" + rectangle2.getArea());
		System.out.println("周长：" + rectangle2.getPerimeter());
	}

	/**
	 * 4
	 */
	public static Book[] hw4(int num) {
		Scanner input = new Scanner(System.in);
		Book[] books = new Book[num];
		for (int i = 0; i < books.length; i++) {
			books[i] = new Book();
			System.out.println("请输入第" + (i + 1) + "本书的编号:");
			books[i].id = input.nextInt();
			System.out.println("请输入第" + (i + 1) + "本书的书名:");
			books[i].title = input.next();
			System.out.println("请输入第" + (i + 1) + "本书的单价:");
			books[i].price = input.nextDouble();
			System.out.println("请输入第" + (i + 1) + "本书的出版社:");
			books[i].publisher = input.next();
		}
		input.close();
		for (Book book : books) {
			book.showMe();
		}
		return books;
	}

	/**
	 * 5
	 */
	public static void hw5() {
		Book[] books = hw4(5);
		double temp = books[0].price;
		int index = 0;
		for (int i = 1; i < books.length; i++) {
			if (books[i].price > temp) {
				temp = books[i].price;
				index = i;
			}
		}
		System.out.print("单价最高的书是第" + (index + 1) + "本，详细信息如下：\n");
		books[index].showMe();
	}

	/**
	 * 6
	 */
	public static Book[] hw6(int num) {
		Scanner input = new Scanner(System.in);
		Book[] books = new Book[num];
		boolean flag;
		int temp;
		for (int i = 0; i < books.length; i++) {
			books[i] = new Book();
			flag = false;
			do {
				if (flag) {
					System.out.println("编号重复，请重新输入第" + (i + 1) + "本书的编号:");
				} else {
					System.out.println("请输入第" + (i + 1) + "本书的编号:");
				}
				temp = input.nextInt();
				flag = false;
				for (int j = 0; j < i; j++) {
					if (books[j].id == temp) {
						flag = true;
						break;
					}
				}
			} while (flag);
			books[i].id = temp;
			System.out.println("请输入第" + (i + 1) + "本书的书名:");
			books[i].title = input.next();
			System.out.println("请输入第" + (i + 1) + "本书的单价:");
			books[i].price = input.nextDouble();
			System.out.println("请输入第" + (i + 1) + "本书的出版社:");
			books[i].publisher = input.next();
		}
		input.close();
		for (Book book : books) {
			book.showMe();
		}
		return books;
	}

	/**
	 * 7
	 */
	public static void hw7() {
		Scanner input = new Scanner(System.in);
		Book[] books = hw6(3);
		int temp, index = -1;
		System.out.println("请输入要查询书籍的编号:");
		temp = input.nextInt();
		input.close();
		for (int i = 0; i < books.length; i++) {
			if (books[i].id == temp) {
				index = i;
			}
		}
		if (index == -1) {
			System.out.print("要查询的书籍不存在！\n");
		} else {
			System.out.print("要查询的书籍存在，详细信息如下：\n");
			books[index].showMe();
		}
	}

	/**
	 * 8
	 */
	public static void hw8() {
		Book[] books = hw6(5);
		double temp = 0;
		for (int i = 0; i < books.length; i++) {
			temp += books[i].price;
		}
		System.out.print("输入书籍的平均单价为：" + (temp / books.length) + "元。\n");
	}

	/**
	 * 按书籍价格降序排列
	 */

	private static void swap(Book[] books, int index1, int index2) {
		Book book = new Book();
		if (books[index1].price < books[index2].price) {
			book = books[index1];
			books[index1] = books[index2];
			books[index2] = book;
		}
	}

	/**
	 * 9 选择排序
	 */
	public static void hw90() {
		Book[] books = hw6(3);
		for (int i = 0; i < books.length - 1; i++) {
			for (int j = i + 1; j < books.length; j++) {
				swap(books, i, j);
			}
		}
		for (Book book1 : books) {
			book1.showMe();
		}
	}

	/**
	 * 9 冒泡排序
	 */
	public static void hw91() {
		Book[] books = hw6(3);
		for (int i = 0; i < books.length - 1; i++) {// 每次最小元素就像气泡一样"浮"到数组的最后
			for (int j = 0; j < books.length - 1 - i; j++) {
				swap(books, j, j + 1);// 依次比较相邻的两个元素,使较小的那个向后移
			}
		}
		for (Book book1 : books) {
			book1.showMe();
		}
	}

	/**
	 * 9 鸡尾酒排序
	 */
	public static void hw92() {
		Book[] books = hw6(3);
		int left = 0, right = books.length - 1;// 初始化边界
		while (left < right) {
			// 前半轮,将最小元素放到后面
			for (int j = left; j < right; j++) {
				swap(books, j, j + 1);
			}
			right--;
			// 后半轮,将最大元素放到前面
			for (int j = right; j > left; j--) {
				swap(books, j - 1, j);
			}
			left++;
		}
		for (Book book1 : books) {
			book1.showMe();
		}
	}
}
